package com.scalar.chiptrack.operations.tapereel.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequestLot;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.ScalarException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 18, 2007
 * Time: 6:43:58 PM
 */
public class TapeReelRequestSaveCommand  extends ActionCommand
{
    private static final String ACTION_NAME = "/tapeReelRequest.do";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ScalarException
    {
        String actionType                       = request.getParameter("actionType");
        TapeReelRequestForm tapeReelRequestForm = (TapeReelRequestForm) form;
        TapeReelRequest tapeReelRequest         = tapeReelRequestForm.getTapeReelRequest();
        ActionForward actionForward             = mapping.findForward("confirmRequest");
        HttpSession mSession                    = request.getSession();

        User user       = (User) mSession.getAttribute(SessionKeys.USER_KEY);
        HashMap errors  = new HashMap();
        HashMap info    = new HashMap();
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.TAPE_REEL_REQUEST, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        try 
		{
            request.setAttribute("isConfirm", "true");
            checkPulledQuantity( tapeReelRequestForm, info );
            actionForward = mapping.findForward("confirmRequest");
        }
        catch (Exception e)
        {
            errors.put("Could not save the Lot Information", null);
        }
        if (!errors.isEmpty())
        {
            request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);
        }

        if (!info.isEmpty())
        {
            request.setAttribute(SessionKeys.INFO_MESSAGES, info);
        }

        request.setAttribute( "actionType", actionType );

        return actionForward;
    }

	/**
	 * @param tapeReelRequestForm
	 * @param info
	 */
	private void checkPulledQuantity(TapeReelRequestForm tapeReelRequestForm, HashMap info) 
	{
		TapeReelRequest tapeReelRequest = tapeReelRequestForm.getTapeReelRequest();
		TapeReelRequestLot tapeReelRequestLot = null;
		String lotNumbers = "";
		
		if( tapeReelRequest.getReqStatus() != null && tapeReelRequest.getReqStatus().equalsIgnoreCase( "Pulled" ) )
		{
			Iterator tapeReelReqLotListIterator = tapeReelRequest.getLotList().iterator();
            while( tapeReelReqLotListIterator.hasNext() )
            {
                tapeReelRequestLot = ( TapeReelRequestLot ) tapeReelReqLotListIterator.next();
                if( tapeReelRequestLot.getPulledQty() > 0 )
                {
                	if( tapeReelRequestLot.getPulledQty() - tapeReelRequestLot.getTrQty() - tapeReelRequestLot.getReturnQty() - tapeReelRequestLot.getRejectQty() != 0 )
                	{
                		lotNumbers = lotNumbers + tapeReelRequestLot.getLotNumber() + ","; 
                	}
                }
            }
		}
		
		if( lotNumbers != null && lotNumbers.length() > 0 )
		{
			
			lotNumbers = "Pulled Qty is more than the T&R Qty + Return Qty + Reject Qty for Lot Number " + lotNumbers.substring(0, (lotNumbers.length() -1 ) );
			info.put( lotNumbers, null );
		}
	}
}
